//index.js
//获取应用实例
let app = getApp();
let wechat = require("../../utils/wechat");
let amap = require("../../utils/amap");
let markersData = [];
Page({
  data: {
    markers:[{
      id:0,
      longitude: 115.504249,
      latitude: 33.86559,
      iconPath: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
      width:32,
      height:32
    }],
    latitude: '33.86559', //115.504249,33.86559
    longitude: '115.504249',
    textData: {},
    user_location: {},
    city: '',
    markerId: 0,
    controls: [
      {
        id: 0,
        position: {
          left: 10,
          top: 200,
          width: 40,
          height: 40
        },
        iconPath: "/images/circle1.png",
        clickable: true
      }
    ]
  },
  onLoad(e) {

    
    amap.getRegeo()
      .then(d => {
        console.log('-------------')
        console.log(d);
        let { name, desc, latitude, longitude } = d[0];
        let { city } = d[0].regeocodeData.addressComponent;
        this.setData(this.data.user_location={
          'city':city,
          'latitude':latitude,
          'longitude':longitude,
          'textData': { name, desc }
        })
        // user_location
        console.log(this.data.user_location)
      })
      .catch(e => {
        console.log(e);
      })
      // this.addMarker()
  },
  // addMarker() {
  //     marker = amap.marker({
  //       icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  //       position: [115.504249,33.86559],
        
  //     });
  //     var mpCtx = wx.createMapContext("map");
  //     mpCtx.moveToLocation();
  //     marker.setMap(mpCtx);
  //   },
    bindInput() {
      let {
        latitude,
        longitude,
        city
      } = this.data;
      let url = `/pages/inputtip/inputtip?city=${city}&lonlat=${longitude},${latitude}`;
      wx.navigateTo({
        url
      });
    },
  makertap(e) {
    // console.log(e);
    let { markerId } = e;
    let { markers } = this.data;
    let marker = markers[markerId];
    // console.log(marker);
    this.showMarkerInfo(marker);
    this.changeMarkerColor(markerId);
  },
  showMarkerInfo(data) {
    let { name, address: desc } = data;
    this.setData({
      textData: { name, desc }
    })
  },
  changeMarkerColor(markerId) {
    let { markers } = this.data;
    markers.forEach((item, index) => {
      item.iconPath = "/images/marker.png";
      if (index == markerId) item.iconPath = "/images/marker_checked.png";
    })
    this.setData({ markers, markerId });
  },
  getRoute() {
    // 起点
    let { latitude, longitude, markers, markerId, city, textData } = this.data;
    let { name, desc } = textData;
    if (!markers.length) return;
    // 终点
    let { latitude: latitude2, longitude: longitude2 } = markers[markerId];
    let url = `/pages/routes/routes?longitude=${longitude}&latitude=${latitude}&longitude2=${longitude2}&latitude2=${latitude2}&city=${city}&name=${name}&desc=${desc}`;
    wx.navigateTo({ url });
    


    // let plugin = requirePlugin('routePlan');
    // let key = 'OTCBZ-YUC6U-PGEVA-4ZA6U-VAWMS-VLFJ3';  //使用在腾讯位置服务申请的key
    // let referer = '高德地图';   //调用插件的app的名称
    // let endPoint = JSON.stringify({  //终点
    //   'name': '北京西站',
    //   'latitude': 39.894806,
    //   'longitude': 116.321592
    // });
    // let navigation = 1 //添加导航功能
    // wx.navigateTo({
    //   url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint+'&navigation='+navigation
    // });

  },
  clickcontrol(e) {
    console.log("回到用户当前定位点");
    let { controlId } = e;
    let mpCtx = wx.createMapContext("map");
    mpCtx.moveToLocation();
  },
  mapchange() {
    // console.log("改变视野");
  }
})
